package com.yunduansing.core.controller;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yunduansing.core.commons.DbCommon;
import com.yunduansing.core.config.constant.SysConstant;
import com.yunduansing.core.dao.redis.JedisClient;
import com.yunduansing.core.dto.AccountDto;
import com.yunduansing.core.dto.BaseSearch;
import com.yunduansing.core.model.AccountModel;
import com.yunduansing.core.model.SysUser;
import com.yunduansing.core.service.AccountService;
import com.yunduansing.core.service.GroupService;
import com.yunduansing.core.service.RoleService;
import com.yunduansing.core.utils.CommonUtil;
import com.yunduansing.core.utils.ResultUtil;

@Controller
@RequestMapping("/manager/account")
public class AccountController {
	@Autowired
	private AccountService service;
	@Autowired
	private GroupService groupService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private JedisClient jedis;
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView index(Model m){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/account/index");
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/page",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String pagination(DbCommon.Grid grid,@RequestParam(value="search",required=false) BaseSearch search){
		return CommonUtil.toJsonWithGson(service.pagination(grid, search));
	}
	
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public ModelAndView detail(Model m,@RequestParam(value="id",required=false) Integer id){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/account/detail");
		AccountDto entity;
		if(id==null)
			entity=new AccountDto();
		else
			entity=service.getAccountWithGroupAndRole(id);
		m.addAttribute("model", entity);
		m.addAttribute("roleList", roleService.getAll());
		m.addAttribute("groupList", groupService.getAll());
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String save(AccountModel model,@RequestParam(value="groupIds[]",required=false) List<Integer> groupIds,
			@RequestParam(value="roleIds[]",required=false) List<Integer> roleIds){
		return CommonUtil.toJsonWithGson(service.saveWithGroupAndRole(model,groupIds,roleIds));
	}
	
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String delete(@RequestParam(value="ids[]") List<Object> ids){
		return CommonUtil.toJsonWithGson(service.deleteByKeys(ids));
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public ModelAndView login(Model m){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/account/login");
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/login",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String login(@ModelAttribute("ssm") AccountModel account,HttpSession httpSession){
		ResultUtil result=service.login(account.getAccount(), account.getPassword());
		if(result.isFlag()){
			httpSession.setAttribute("CurrentUser", result.getData());
			//redis保存用户信息
			SysUser user=(SysUser)result.getData();
			jedis.hset(SysConstant.REDIS_SYS_USER_KEY+httpSession.getId(), "account", user.getAccount());
			jedis.hset(SysConstant.REDIS_SYS_USER_KEY+httpSession.getId(), "name", user.getName());
			jedis.hset(SysConstant.REDIS_SYS_USER_KEY+httpSession.getId(), "nickname", user.getNickname());
			//设置用户的登录有效期
			jedis.expire(SysConstant.REDIS_SYS_USER_KEY+httpSession.getId(), SysConstant.REDIS_SYS_USER_EXPIRE);
		}
		return CommonUtil.toJsonWithGson(result);
	}
	
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String logout(HttpSession httpSession){
		httpSession.setAttribute("CurrentUser", null);
		//从redis中移除当前用户的登录状态
		jedis.del(SysConstant.REDIS_SYS_USER_KEY+httpSession.getId());
		jedis.expire(SysConstant.REDIS_SYS_USER_KEY+httpSession.getId(), 0);
		return "redirect:login";
	}
	
	@RequestMapping(value="/resetPassword",method=RequestMethod.GET)
	public String resetPassword(){
		return "/beetl/account/resetPassword";
	}
	
	@RequestMapping(value="/resetPassword",method=RequestMethod.POST)
	public String resetPassword(ModelMap m,String newPwd,String oldPwd,HttpSession httpSession){
		AccountModel user=(AccountModel) httpSession.getAttribute("CurrentUser");
		return CommonUtil.toJsonWithGson(service.resetPassword(newPwd, oldPwd, user.getId()));
	}
}
